<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 表单上可以写method -->
    <form action="http://ajax-api.itheima.net/api/data" method="post">
        <!-- name属性加上之后，就可以设置属性名 -->
        <input name="username" type="text"  placeholder="请输入用户名" />
        <br /> 
        <input name="food" type="text" placeholder="请输入喜欢的食物" />
        <br />
        <input name="sign" type="textarea" placeholder="请输入个性签名" />
        <br />
        <!-- 表单里面，按钮，如果不写type，默认是type是submit -->
        <button>提交</button>
        <!-- <input type="submit"> -->
      </form>
      <script src="./lib/axios.js"></script>
      <script src="./lib/form-serialize.js"></script>
      <script>

        // 1. 如果是按钮提交表单
        // 如果按钮没有加type， 要用ajax提交数据的话，先阻止
        const btn = document.querySelector('button')
        // btn.addEventListener('click', function(e){
        //     e.preventDefault()
        // })


        // 2. 如果是form表单的submit
        const form = document.querySelector('form')
        // 属性选择器
        const username = document.querySelector('[name="username"]')
        const food = document.querySelector('[name="food"]')
        const sign = document.querySelector('[name="sign"]')

        form.addEventListener('submit', function(e){
            e.preventDefault()

            const res = serialize(form)
            console.log(res)
            axios({
                url:'http://ajax-api.itheima.net/api/data',
                method: 'POST',
                // data:{
                //     username:username.value,
                //     food:food.value,
                //     sign:sign.value
                // }
                // application/x-www-form-urlencoded
                data:res  // username=123&food=123123&sign=1231231
            }).then(res => {
                console.log(res)
            })

        })
      </script>
</body>
</html>